import basic from '../model/basic.js';
import sysdata from '../model/sysdata.js'; 
var actmodel = new basic();

function getaddrmodel(addrcodedata, name) {
  for (var a in addrcodedata) {
    if (addrcodedata[a].name.indexOf(name) != -1) {
      return { val: { id: a, name: addrcodedata[a].name }, sub: addrcodedata[a].sub }
    }


  }

  return null;

}

actmodel.wxaddrtosysaddr = function (addr) {



  return new Promise((resolve) => {
    sysdata.citys().then(function (obj) {
      if (obj["status"] == 1) {
      var   addrdata = obj["obj"];
        var reslut = []
        var provinceName = addr.provinceName.replace("省", "");
        var provincemodel = getaddrmodel(addrdata, provinceName)
        if (provincemodel != null) {
          reslut.push(provincemodel.val)
          var cityName = addr.cityName.replace("市", "");
          var citymodel = getaddrmodel(provincemodel.sub, cityName)
          if (citymodel != null) {
            reslut.push(citymodel.val)
            var countyName = addr.countyName
            countyName = countyName.replace("市", "")
            countyName = countyName.replace("区", "")
            countyName = countyName.replace("县", "")
            var countyModel = getaddrmodel(citymodel.sub, countyName)
            if (countyModel != null) {
              reslut.push(countyModel.val)
            }
          }

        }
        if(reslut.length>0){
          resolve(reslut)
        }
        else
        {
          resolve(null)
        }


      }





    })
  })




}
actmodel.offset = function (that, reset, para, xcsorts) {
 
  
  if (!xcsorts) {
    xcsorts = [["isdefault", "desc"], ["modifytime", "desc"]];
  }

  var acturlpara = {};
  acturlpara["url"] = "entry/wxapp/sys_addr"
  if (para) {
    para["op"] = "offset"
  }
  else {
    this.wherepara["op"] = "offset"
  }
  return actmodel.pagelist(acturlpara, that, reset, para, xcsorts);
}


actmodel.getphone = function (encryptedData, iv,code) {
  var app = getApp();
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/sys_addr',
      showLoading: false,
      method: 'POST',
      data: {
        op: 'getphone',
        encryptedData: encryptedData,
        iv: iv,
        code:code
      },
      success(res) {
        if (res.data.errno == 0) {
          var resultmodel = res.data.data;
          if (resultmodel["status"] == "41009") {
            wx.setStorageSync('userInfo', '');
            app.util.getUserInfo(function (params) {
              wx.showToast({
                title: '请重新授权',
              })

            })
          }
          else {
            resultmodel["code"] = "success";

          }
          resolve(resultmodel)

        }
        else {
          reject({ code: "faill" })
        }

      },
      fail(res) {
        reject({ code: "faill" })
        //res.data.message
      }
    })
  })
}

actmodel.addormodify = function (model) {
  var app = getApp();
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/sys_addr',
      showLoading: false,
      method: 'POST',
      data: {
        op: 'addormodify',
        model: JSON.stringify(model)
      },
      success(res) {
        if (res.data.errno == 0) {
          var resultmodel = res.data.data;
          resultmodel["code"] = "success";
          resolve(resultmodel)
        }
        else {
          reject({ code: "faill" })
        }

      },
      fail(res) {
        reject({ code: "faill" })
        //res.data.message
      }
    })
  })
}

actmodel.del = function (ids) {
  var app = getApp();
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/sys_addr',
      showLoading: false,
      method: 'POST',
      data: {
        op: 'del',
        ids: ids
      },
      success(res) {
        if (res.data.errno == 0) {
          var resultmodel = res.data.data;
          resultmodel["code"] = "success";
          if (resultmodel["status"] == 1) {
            actmodel.syspage.offset = actmodel.syspage.offset - resultmodel["delnum"];
          }

          resolve(resultmodel)
        }
        else {
          reject({ code: "faill" })
        }

      },
      fail(res) {
        reject({ code: "faill" })
        //res.data.message
      }
    })
  })
}
actmodel.setdefalut = function (id, isdefault) {
  var app = getApp();
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/sys_addr',
      showLoading: false,
      method: 'POST',
      data: {
        op: 'setdefalut',
        id: id,
        isdefault: isdefault
      },
      success(res) {
        if (res.data.errno == 0) {
          var resultmodel = res.data.data;
          resultmodel["code"] = "success";


          resolve(resultmodel)
        }
        else {
          reject({ code: "faill" })
        }

      },
      fail(res) {
        reject({ code: "faill" })
        //res.data.message
      }
    })
  })
}

actmodel.mydefalut = function (navcur,isclub,lat,lng) {
  var app = getApp();

  var data=  {
    op: 'mydefalut',
    navcur
  };
  if(isclub==1){
    data["isclub"]=isclub;
    data["lat"]=lat;
    data["lng"]=lng;
  }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/sys_addr',
      showLoading: false,
      method: 'POST',
      data,
      success(res) {
        if (res.data.errno == 0) {
          var resultmodel = res.data.data;
          resultmodel["code"] = "success";
          resolve(resultmodel)
        }
        else {
          reject({ code: "faill" })
        }

      },
      fail(res) {
        reject({ code: "faill" })
        //res.data.message
      }
    })
  })
}

module.exports = actmodel;
